# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>
# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: Alexander Baldeck <alexander@archlinux.org>
# Contributor: Dale Blount <dale@archlinux.org>
# Contributor: Anders Bostrom <anders.bostrom@home.se>

pkgbase=thunderbird
pkgname=(thunderbird)
pkgver=102.11.0
pkgrel=1
pkgdesc='Standalone mail and news reader from mozilla.org'
url='https://www.mozilla.org/thunderbird/'
arch=(x86_64)
license=(MPL GPL LGPL)
depends=(
  glibc gtk3 libgdk-3.so libgtk-3.so mime-types dbus libdbus-1.so dbus-glib
  alsa-lib nss hunspell sqlite ttf-font libvpx libvpx.so zlib bzip2 libbz2.so
  botan2 libwebp libwebp.so libwebpdemux.so libevent libjpeg-turbo libffi
  libffi.so nspr gcc-libs libx11 libxrender libxfixes libxext libxcomposite
  libxdamage pango libpango-1.0.so cairo gdk-pixbuf2 icu libicui18n.so
  libicuuc.so freetype2 libfreetype.so fontconfig libfontconfig.so glib2
  libglib-2.0.so pixman libpixman-1.so gnupg json-c libcanberra
)
makedepends=(
  unzip zip diffutils python nasm mesa libpulse libice libsm
  rust clang llvm cbindgen nodejs lld
  gawk perl findutils libotr wasi-compiler-rt wasi-libc wasi-libc++ wasi-libc++abi
)
options=(!emptydirs !makeflags !lto)
source=(https://archive.mozilla.org/pub/thunderbird/releases/$pkgver/source/thunderbird-$pkgver.source.tar.xz{,.asc}
        thunderbird.desktop
        vendor-prefs.js
        distribution.ini
        mozconfig.cfg
        metainfo.patch
        rustc_version-0.4.0.patch
        gcc-13.patch)
validpgpkeys=(
  14F26682D0916CDD81E37B6D61B7B526D98F0353 # Mozilla Software Releases <release@mozilla.com>
  4360FE2109C49763186F8E21EBE41E90F6F12F6D # Mozilla Software Releases <release@mozilla.com>
)

# Google API keys (see http://www.chromium.org/developers/how-tos/api-keys)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact foutrelis@archlinux.org for
# more information.
_google_api_key=AIzaSyDwr302FpOSkGRpLlUpPThNTDPbXcIn_FM

# Mozilla API keys (see https://location.services.mozilla.com/api)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact heftig@archlinux.org for
# more information.
_mozilla_api_key=16674381-f021-49de-8622-3021c5942aff

prepare() {
  cd $pkgname-$pkgver

  echo "${noextract[@]}"

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    [[ $src = *.patch ]] || continue
    msg2 "Applying patch $src..."
    patch -Np1 < "../$src"
  done

  printf "%s" "$_google_api_key" >google-api-key
  printf "%s" "$_mozilla_api_key" >mozilla-api-key
  cp ../mozconfig.cfg .mozconfig
  sed "s|@PWD@|${PWD@Q}|g" -i .mozconfig
}

build() {
  cd $pkgname-$pkgver
  if [[ -n "${SOURCE_DATE_EPOCH}" ]]; then
    export MOZ_BUILD_DATE=$(date --date "@${SOURCE_DATE_EPOCH}" "+%Y%m%d%H%M%S")
  fi
  export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=none
  export MOZBUILD_STATE_PATH="${srcdir}/mozbuild"
  ./mach configure
  ./mach build
  ./mach buildsymbols
}

package_thunderbird() {
  optdepends=(
    'libotr: OTR support for active one-to-one chats'
    'libnotify: Notification integration'
  )

  cd $pkgname-$pkgver
  DESTDIR="$pkgdir" ./mach install

  install -Dm 644 ../vendor-prefs.js -t "$pkgdir/usr/lib/$pkgname/defaults/pref"
  install -Dm 644 ../distribution.ini -t "$pkgdir/usr/lib/$pkgname/distribution"
  install -Dm 644 ../thunderbird.desktop -t "$pkgdir/usr/share/applications"
  install -Dm 644 comm/mail/branding/thunderbird/net.thunderbird.Thunderbird.appdata.xml \
    "$pkgdir/usr/share/metainfo/net.thunderbird.Thunderbird.appdata.xml"

  for i in 16 22 24 32 48 64 128 256; do
    install -Dm644 comm/mail/branding/thunderbird/default${i}.png \
      "$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/$pkgname.png"
  done
  install -Dm644 comm/mail/branding/thunderbird/TB-symbolic.svg \
    "$pkgdir/usr/share/icons/hicolor/symbolic/apps/thunderbird-symbolic.svg"

  # Use system-provided dictionaries
  ln -Ts /usr/share/hunspell "$pkgdir/usr/lib/$pkgname/dictionaries"
  ln -Ts /usr/share/hyphen "$pkgdir/usr/lib/$pkgname/hyphenation"

  # Install a wrapper to avoid confusion about binary path
  install -Dm755 /dev/stdin "$pkgdir/usr/bin/$pkgname" <<END
#!/bin/sh
exec /usr/lib/$pkgname/thunderbird "\$@"
END

  # Replace duplicate binary with wrapper
  # https://bugzilla.mozilla.org/show_bug.cgi?id=658850
  ln -srf "$pkgdir/usr/bin/$pkgname" \
    "$pkgdir/usr/lib/$pkgname/thunderbird-bin"
}

_package_i18n() {
  pkgdesc="$2 language pack for Thunderbird"
  depends=("thunderbird>=$pkgver")
  install -Dm644 thunderbird-i18n-$pkgver-$1.xpi \
    "$pkgdir/usr/lib/thunderbird/extensions/langpack-$1@thunderbird.mozilla.org.xpi"
}

_languages=(
  'af     "Afrikaans"'
  'ar     "Arabic"'
  'ast    "Asturian"'
  'be     "Belarusian"'
  'bg     "Bulgarian"'
  'br     "Breton"'
  'ca     "Catalan"'
  'cak    "Kaqchikel"'
  'cs     "Czech"'
  'cy     "Welsh"'
  'da     "Danish"'
  'de     "German"'
  'dsb    "Lower Sorbian"'
  'el     "Greek"'
  'en-GB  "English (British)"'
  'en-US  "English (US)"'
  'es-AR  "Spanish (Argentina)"'
  'es-ES  "Spanish (Spain)"'
  'et     "Estonian"'
  'eu     "Basque"'
  'fi     "Finnish"'
  'fr     "French"'
  'fy-NL  "Frisian"'
  'ga-IE  "Irish"'
  'gd     "Gaelic (Scotland)"'
  'gl     "Galician"'
  'he     "Hebrew"'
  'hr     "Croatian"'
  'hsb    "Upper Sorbian"'
  'hu     "Hungarian"'
  'hy-AM  "Armenian"'
  'id     "Indonesian"'
  'is     "Icelandic"'
  'it     "Italian"'
  'ja     "Japanese"'
  'ka     "Georgian"'
  'kab    "Kabyle"'
  'kk     "Kazakh"'
  'ko     "Korean"'
  'lt     "Lithuanian"'
  'ms     "Malay"'
  'nb-NO  "Norwegian (Bokmål)"'
  'nl     "Dutch"'
  'nn-NO  "Norwegian (Nynorsk)"'
  'pa-IN  "Punjabi (India)"'
  'pl     "Polish"'
  'pt-BR  "Portuguese (Brazilian)"'
  'pt-PT  "Portuguese (Portugal)"'
  'rm     "Romansh"'
  'ro     "Romanian"'
  'ru     "Russian"'
  'sk     "Slovak"'
  'sl     "Slovenian"'
  'sq     "Albanian"'
  'sr     "Serbian"'
  'sv-SE  "Swedish"'
  'th     "Thai"'
  'tr     "Turkish"'
  'uk     "Ukrainian"'
  'uz     "Uzbek"'
  'vi     "Vietnamese"'
  'zh-CN  "Chinese (Simplified)"'
  'zh-TW  "Chinese (Traditional)"'
)
_url=https://archive.mozilla.org/pub/thunderbird/releases/$pkgver/linux-x86_64/xpi

for _lang in "${_languages[@]}"; do
  _locale=${_lang%% *}
  _pkgname=thunderbird-i18n-${_locale,,}

  pkgname+=($_pkgname)
  source+=("thunderbird-i18n-$pkgver-$_locale.xpi::$_url/$_locale.xpi")
  eval "package_$_pkgname() {
    _package_i18n $_lang
  }"
done

# Don't extract languages
noextract=()
for _src in "${source[@]%%::*}"; do
    case "$_src" in 
      *.xpi) noextract+=("$_src") ;;
    esac
done

sha512sums=('8ae12ffab5893456f14df7417e1a9d5e0e754bdd743b2f3a42623ea57342f10de32763e56ff9ccf6938f89726e31d2a4062d4336a77918e884aac70a338f4130'
            'SKIP'
            'a0061fcb2a7f66061e336a8d95948592f56f4752e56467f14ba63846720ebf845cce7511d1a2637e3b80d5a1ffdaa2fb783fa37195103425ef65222d45372012'
            '6918c0de63deeddc6f53b9ba331390556c12e0d649cf54587dfaabb98b32d6a597b63cf02809c7c58b15501720455a724d527375a8fb9d757ccca57460320734'
            '5cd3ac4c94ef6dcce72fba02bc18b771a2f67906ff795e0e3d71ce7db6d8a41165bd5443908470915bdbdb98dddd9cf3f837c4ba3a36413f55ec570e6efdbb9f'
            'a34dd97954f415a5ffe956ca1f10718bd164950566ceba328805c2ccbb54ed9081df07f2e063479bf932c4a443bb5b7443cca2f82eea3914465ed6e4863e0c0e'
            '7e43b1f25827ddae615ad43fc1e11c6ba439d6c2049477dfe60e00188a70c0a76160c59a97cc01d1fd99c476f261c7cecb57628b5be48874be7cf991c22db290'
            '36d9662fc94cbf7dcf371adc13a9cda679bc75df961d86de019d3c8ebb0be3062d5ef762d175fab58696db74758100a65de45d7832e0e2bd4e15c901f72d8349'
            '3fbbcdb2cdce00156ec54d517d4709203b5324cc73b02df32be3891eed24c37d8ea4ac8acee3b462da340ef7e2addd602575f350b10d830cf3eefb707e849794'
            '485ae20825e7d30d6a2454b6d5013c6cb16e539cc7170f0c324dd39225206b8313cd344db19b3ee162873322537d9ee614b754f931580681fffb462c060c4774'
            'c39fa28a6675f9d7045351513a54d30fb6d2a37db3a84e89a569ad021d877d16b14d79cd012e46c44d2cc9d2eb80a4a25739deb420f7d9a79fa9ba2b3131365c'
            'c73e3e3f9aacbb6ac8862f1baa974517051a2e27975e452486b701fb5116bc20e88f6366d73a6ca1d0633abc8047530b2353321be6af2f85f78298dd64b4d3d8'
            '1949e53afbe0680d9d16703c0d80d5cd2401780ca1333810f1c20fc8cbaff3e6c36ab4e6fcbaa0216f58927bf27199a9a3608374d31e3b5ddda59a1c5a7b309a'
            'a6b2e49b3973bc2c70f344c63eff3906269d79aa1a5fbe0e42868af41f28f044789b4ed0e22102f470b1d8452dd928dcbd98ebe1e97c25e8150bbc73da0a6632'
            '9f119cd61e4103b55f8503d6657b9e614c75a1423d075621be0942a13c7a90b9a1cc8e6f263c543c172cba605f2df39d66ec7cbd90292526349d758b2ff45f38'
            'b1eb1d946e093594080e35d7c5bb0a637c1c79bc29bc8f33ea447a73fbbe5dd424d7752437cf15527a9301191ce4a560b9eb2e25582c1f020f9c298722730c8d'
            '21b227b97768d29787cb362d782fa1d92f70d57e65bbec1f9f3a4c66001a8fef66f54e6de5c18c4418c369033ed4c13bc1b4ce275094a633bb67072dfe94b80a'
            '2e7d1c3dcdc348029d327c5fb0bf909c5b0b48e7f40f03b29baa53c13c0cad0f9d0a692d03f69fc58888c05a16bca2709eacfbcbe3e9917b28c049918aaa3356'
            '43e02462959c5fcb0a2176151f5d49851c4f9f379711efbb7bc0fd4504f0f6788840f01236d10909cefac0b1277a0358cebb411b50303c232b930da0297704ed'
            '78155b89d4de4855d7d967f9489fe5aa52fce6a55b8e185e0246eb400b9f21a1ab35840c3904eb539284693e287e9a5523ada6d250df96085c85bd9e48fd9c2e'
            'dc42fb48f625b375f90d772b97e898a2751eec2b04d24269cdf09cf5ebe6c77cf73eaf0b84b8d59754e54631e7e205598783038371d44ef0dbe429125330a1b5'
            '513fdda538c20ef9022af6d160ca44919221cf82ae651d0d90356d734e9cb0aab95d2105d72e65ed28e4df1da472bfaf157f0e3888a200cb65b352b66e2418b4'
            '32f453951156502e742841abf314d4b223af1c3e8359b3c0d927ceaf48841a1f5ddfdac36eb8709801c32feae06ed0f1675c08b58da5f8d795aa9a47987e6c0e'
            'f60a8b2f930515c61390b39cb9ad50d7d73f9988ec34d4c71918669198e521667a70e469477042c62f47cd90cffe233bb3a2cbb172c99675c023b257ddf3ac7e'
            '9a4447b30cdb2b3e7775ea7cd8effef1c0f3a45dceee70d5a8b1c6313567baa89bcbc7b60a39d0fe375fff3cf193b06d8222d2d4e2fcca288340d797272a1c86'
            '3cc16180fb1666b86de9b81bb9e730894d615db22c20a75c7282c63cc05be65bca4997dc1bd331df17ddd9bf05908e014c1e0dd70e61cf985b8321394b476044'
            'ed5f2a041ff31dc0d0406439548c3a753d122e296d5fbbbd48369a47d482340a399beb0be691e21b2f2a70de551939483ed3a8955c7bb5c425a35eb42ae21f53'
            '4513a1a3dee098b56687d8966db073be4e69d56bc76a8ec8bfb519122fd83384ec09367d48b84012495763433d47ea5faa2cbdab1233097c7214390c1c00c160'
            '4591a6faf52e259e19a6ce2159904c1f4f018dd1e95883015988a8acf8882d3b10914c139bba761fd675c76aed4086b4887b92698836ac3f9cc593a3d898c0bf'
            '68584b3abe323440d84ce25147cc3fce7f71c5df0cb6778fbfbc48b2dc9c201363c22089658e422a5f296dd85e8f236ba7b2184b1eb5473cf1d0f43892bc1ca6'
            '3ae2910cbbd4dae977bb7056b36ddabec4562f5642bb347ee70075309744010a71a7b7274171408f353d793c4c1be5e072b83452640d141a0621bc79d98e8189'
            'd75e4cbcf4db3446a756bde3344d83ed1d4130485a0074f5a1c2f3de030e1a5a3cbd04d7a03666dde942f65977c8fffccec6f01f936bb4b967018ef01ad0c811'
            '6f79958ba17849e852bda0eb47f88a47e26147f92d23113ab77e2c97ea48955ef05ffd9fc6d6a25876871d4e239b002bcbc82a55abe7a32ac840988d4eadab11'
            'e09abe8d6dcf9253f999306b2ade4cd2e1aabf979530bbbb9bff5b8305322d2d858624201394f58ff86a0fbfee00d0dc9062122af8005a9799f44d0334d9db00'
            'a4c6cf5c71e632bff8640c78335c0052e5a2b43797a9e87137c369c9373ee318238ab6f99bed3ea7a6954c3a874ad4cc950286bbe31a3e3da87d17ccc17c7760'
            'c3875b4f13b9bd8dbb6c2d3ab0b023e39135d5f71262b1b98d6bf31654d1f750a24e437f9050cf5940fe6df7ffe3da7c3aa3fd62e24b89176a1f58277222aa3b'
            '4f345f3aabdc84db77727481a02bc137b00ea29ee61bb28f3a0af75dd4d7505ddb2e45afeeaa308be20caf569cd634cd4512b3b1dcbc6da50612f3201cc53778'
            'c90e1c8cca6f1581f4d6db7aea8a5c79feb1a5ae98191c57c37a88d065642dcdc8807aa7997ad37699eee8a2433861ab8e27b0d6d19e8ce2a52246fedad0c986'
            '565a8a14f9fb92679b172c20cc9aed805d658f64c109245f69317734dd886bbf202fc3b7cbb52763b315bef9244fea3507edbeecadda884772abe597210e2623'
            'b1b787c3259029151313398a785c7a1ae0ab8b71930e7d446091e30658bd1a59d6c5da9029dcab18338f0ade6ec8356b108882b223fde0ac11cf90bf570452c9'
            '8283c3afbe50f9828d7fe12d4a0ba048289b2ad22ae92bb6f3dcc4bd56be2e0ce802c749dfa27d4ce62bd31df63dc882e9fd8a746abc51dac3eb4377a9fac66e'
            '2fd795d5603dfb1b0f05f4eac75ca7c9dd63ccface8e00ee3f084fdddd567ef4a9b96216d36d9ee603631465a05c626a30a42411212de4b06502b16b51466e84'
            'e9dcb0ecdc3bb4f8437e97450fb34e8eff9db4132a8a9d8750b6b47c57c3ac11150cc41d2c2895a6a629a574eb56c9940b0b9cfd39655e24e1e586360760d402'
            'e9ee9476456e2c3b66112206f1ad589acbf5ca52ed03ca9bdae1b66d75f3487202d401c9ab5ef6b46a79c1b414d6198514548e577825e77a44b8d47893342052'
            '7e2687af06028ea4f6cf71755bd60cc88ea93a3cbbb489466a6bc9598260d4091c0d3b61319a3ab597fddc83646b74577b60ea3717a807d0ed18fb3bb6da46c1'
            'e509076e0dc488d83fd4f147e9c3f5020b1f6dd321d0f3280ef7fd88790417619f6d5cea2431f67c55d2b100a7a95b7a47152186f18de2aed09f7a26e0aab3c4'
            'ad30f62b8cd5055ae3147dbd261bc0abdf04ce53247f9cfe80b7b83f47f4b57214ec0f805760fd25417c44a5eff13c3f86951f73365ddc89284fb3dec6f19a52'
            '60980e6e753f7355406ddf27e140b1195448989903330a9297be5ff5e8760b16a7f745733ad0522f7e27c8e9aedc9cf942271be6f072e1a5acdfaf4f1f140664'
            '5fe4a029b90319bdcd74db24cc2fe2bbf0994aceaf76d721cf940d77f64818ec4d616bc705c9de64f91c36e9e01b93c24a3f3ffd6f246e74de05dea232c9bc1f'
            '8c87a4ce179810ca7488e5ac7be095f40ec210760feaddd2b7112cbcac68a4a1b663cd62102c36a89f2f1d6885d6880c5149efdbdd2d99667b6a4bf144b5cc9a'
            '37d22263bd4c9e74e2e0ff6fabba7e4383e0603b6137f4e807ef6ab65c4ff214f94f79af0e55de58c62742e17843a6ff56b555d768648f68d4c45e0ecfe79094'
            '8be0aecb2ef1e7e8b17440b0abbb6a940586af04e91ccd54a8194bdf9d9da36fbd7bf67c62f44c64a6b63bf99aaa505133d3c953bb1e1b3ffa524dfca3d14fa4'
            'dd8b4190665c73a3c1bbc2ad0cb5abbe5dd8dbdf3dadde8e1d4836d9632adc75445f89b2709171e96932ac313b37272c4143306f0f639f8e62092b97c44f9de7'
            '7be76ec8f983b87cda5e53c95b086620ed14446bdb055786d7a948a4410d88a65d5a9225dfd451b9dcae7b481b10727a97c86680823f566fe58d4b77e8a8e0a4'
            '81758242124ac36aedf98abcefe16c8bf75bd3b0b55efa8a4744de0068c2b48e0df880ba8a1e18257cdf29f6a444acaed88de341bbb5a75a418309d1ef7f4305'
            'c6106198f78a27837bb975bd8ed4dbe4a32963cf708b98b4c3df2a60b6a3d54ff0d00ab0078fad1cf8110caa019e5e4e41a55b3e35169b99b4f1cf6da8762f12'
            'd00c251b43c37fdfe6a2f0812a70f1c0493b184b598ef9578ceb51acf2938f022e905d29713160f852582ff5cb8011c3bf91a3e1fd4b1a3c45dc799f7969da11'
            '499c148a88a78edd5f583566fbf1308b912cbceeca0b99b3414ac6ae844cae03fd796819506dc6215bd738bc82fcf20fcad6f58cbe0ee67b81a9e4d604d064d7'
            '7badffe03ad20cd86c1813ef1f55fc452af1d411be6de877365cd1f6574367f49708930d2eebfe73139ea4177aff3d53b37800b6b9db8f18e8e815b8d29ac3f0'
            'a680309c5222b7d0e460a83be640a02d191ac475bf00147949eee1fd1126561641f02aa25ed333be8570d355d982bea9def0470c94082833642b7e2bbf29a8ec'
            'b15f9c5643168e22941e7d8e2f2104eec947ef1b85c693de31c5f52949732b857e25e5118f557eca1b7bb42fda1eda8bdbfadd2fd4b5e753ec18fd6e586b1129'
            'c60f8ad34846acca23d3e7db7e552a77d1419f9b2a6d036cec6205109395fbe418279a274b71f8e86a493155a71c4b71d255161464e7f28a7a995287f3bbb9ba'
            'bfa48dfd77be93c31c3c1dd8ff2fc2d7ebdcb2ff2739b43c03f2ae1b8ca4edb6b7fd23a7669100070a65e5f36260f5456e60cf50957f9fa2765f42333cdf744c'
            '81f275c7efa39f0bc66311e0ed112b4243777f12a21ac86adba8f660a3bf57f19a328cf264cd5d5467364a8dec420d65e5bb6b6520af36b89170ed7f9eba9807'
            'fc0ba65abb3159fd661f5026722b7ab1b2c8930f2ac5f7ce9ee443638877b6e58aed750e41009b1027dda18265b1a98d247a216e33139aba0e49d035278304a6'
            '9eefe8a7123e82705ae5b0d1d887b00348e2a62c9993209def45e21073338b3d8df462bbf53e153a75b2e881500ee7986a1a441244302f953e7ea5c1ef7b9364'
            '02e31d1072273236d0065749c37aba57eba05134f1347e331a5f7d7cf3ef89d077f68df874bc7910fb17dd4f25a76f1283b5952adcffd748109eb5f4bbf90aca'
            'eb59385c7ccbfc2c0102e3cea41a8b90b1d814e69e7bd3782636b1271b9038d1b96de2d0b1a45b3143451c9c8775e17c902f88b2e9f65276ba2462669c0916d7'
            '537e448414a6bf4fe3cd52ed90ae709d84ef770a785f4ecb559d98376482146251c052e6302abc163d893bd9d593ac84d2304421d1edfe988de5d73f47a8d128'
            'eb2a77d4ea5390705060368357f5dd6327b703dc481a48a9045ada51d6119af3731b87a39a1fd74abd97deb36e860cebfe2640312fc77abd0f40590a1ee83790'
            'ef86ed3c3670c111a2fbf9cec2536d1436287c9784bff1cd6091bd015b387e967e4be44460b6bbf109636d14f2bb5f069ea3ce108f7cbd2cc83485179d9de876'
            '6bf42d9af76ab3389dd775547fe05e955dfe5740aedca60a1b29e652ba47a6a5d86d43c494eb46e48aea5585a9ea991d76fea201a538e27d6e108c6f7fcdd13f')

# vim:set sw=2 et:
